package lyxs_916.drawabletest.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

/**
 * 类说明:绘制圆角的drawable
 * Created by 阳  2017/3/12  17:00
 */
public class RoundDrawable extends Drawable {
    private Paint mPaint;
    private Bitmap mBitmap;
    private RectF mRectF;
    private int mRound;

    /**
     * @param bitmap 位图
     * @param round  圆角的值
     */
    public RoundDrawable(Bitmap bitmap, int round) {
        this.mBitmap = bitmap;
        mPaint = new Paint();
        //着色器
        BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setAntiAlias(true);
        mPaint.setShader(bitmapShader);
        mRound = Math.abs(round);

    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawRoundRect(mRectF, mRound, mRound, mPaint);
    }


    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        mRectF = new RectF(left, top, right, bottom);

    }

    @Override
    public void setAlpha(int i) {
        //设置画笔的透明度
        mPaint.setAlpha(i);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        //设置颜色
        mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        //设置不透明度
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public int getIntrinsicWidth() {
        //返回 图片 实际宽
        return mBitmap.getWidth();
    }

    @Override
    public int getIntrinsicHeight() {
        //返回 图片 实际高
        return mBitmap.getHeight();
    }
}
